package com.gary.spring.webContainer.A01;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author peng_rui
 * @date 2022/7/10
 */
public class MyTokenResolve implements HandlerMethodArgumentResolver {
    @Override
    /**
     * 参数是否支持此注解
     */
    public boolean supportsParameter(MethodParameter methodParameter) {
        Token token = methodParameter.getParameterAnnotation(Token.class);
        return token != null;
    }

    @Override
    /**
     * 完成解析后，需要将解析器添加到 adapter 中， 否则无法生效
     */
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        String token = nativeWebRequest.getHeader("token");
        return token;
    }
}
